<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/pub/head_bootstrap.inc"%>
<title>${module.moduleName}</title>
</head>
<body class="FrameRightBg">
	<!-- 表格列表 -->
	<div class="FrameRight" id="FrameRightContent">
		<div class="FrameRightInner mlzjhzdlb-default">
			<!-- location -->
			<div class="main">
				<div id="standardSearchForm">
					<%@ include file="/WEB-INF/jsp/module/view/new_selector_form.jsp"%>
					<form id="formsearch" method="post">
						<input type="hidden" name="${module.module}list"
							id="${module.module}list" class="field" />
						<div class="selector-form"></div>
					</form>
				</div>

				<div class="common-option">
					<div class="inner">
						<button type="button" class="btn btn-icon" onclick="StandardTable.goDetailPage()"><i class="icon iconfont icon-icon-plus"></i>添加</button>
						<button type="button" class="btn btn-icon" onclick="StandardTable.modifyDetailPage()"><i class="icon iconfont icon-icon-edit"></i>修改</button>
						<button type="button" class="btn btn-icon" onclick="StandardTable.deleteByIds()"><i class="icon iconfont icon-icon-trash"></i>删除</button>
						<button type="button" class="btn btn-icon" onclick="StandardTable.stop()"><i class="icon iconfont bd bd-times-circle-o"></i>停用</button>
						<button type="button" class="btn btn-icon" onclick="StandardTable.start()"><i class="icon iconfont bd bd-caret-right" "></i>启用</button>
						<!-- <button type="button" class="btn btn-icon" onclick="FF_SubmitToCloudEvent()"><i class="icon iconfont icon-icon-plus"></i>上传</button>
						<button type="button" class="btn btn-icon" onclick="FF_downloadFromCloudEvent()"><i class="icon iconfont icon-icon-plus"></i>下载</button>
					<button type="button" class="btn btn-icon" onclick="FF_UpdateClickEvent()"><i class="icon iconfont icon-icon-plus"></i>更新</button> -->
					</div>
				</div>

				<div class="common-table">
					<table id="listTable" class="table table-hover"></table>
				</div>
			</div>
			<!-- main end -->
		</div>
		<!-- FrameRightInner -->
	</div>
	<!-- FrameRight -->
	<script type="text/javascript" src="${baseURL}/js/module/bootstrap/common_list.js"></script>
	<script>
	
	
  	var win = parent || window;
  	var selector;
  	
	StandardTable = {
		/* 跳转详情页 */
		goDetailPage: function(){
			parent.f_addTab("editPlugins", '添加插件', "${baseURL}/install/pluginsinstalledit/superEdit.do?mdId=${param.mdId}&mdCode=${param.mdCode}&menuId=${param.menuId}&op=create&random="+Math.random());
		},
		//修改
		modifyDetailPage: function(){
			var selection = $("#listTable").bootstrapTable('getSelections');
			if(selection.length==1){
				var row = selection[0];
				parent.f_addTab("editPlugins", '修改插件', "${baseURL}/install/pluginsinstalledit/superEdit.do?mdId=${param.mdId}&mdCode=${param.mdCode}&menuId=${param.menuId}&op=update&id="+row.id+"&"+Math.random());
 			}else{
				BT.showWarning("请选中一行！"); 
			}
		},
		/* 停用 */	
		stop: function(){
			var selections = $("#listTable").bootstrapTable('getSelections');
			if(selections.length>0){
				BT.showConfirm("停用","你确定要停用所选的插件吗？",function(){
					var ids = "";
					for(var i=0; i<selections.length; i++){
						ids = ids + selections[i].id + ",";
						if(selections[i].isUsed == 0){
							BT.showWarning("您选择的数据里包括已被停用的插件!");
							return false;
						}
					}
					ids = ids.substring(0,ids.length-1);
					$.ajax({
						type: "POST",
						url:"${baseURL}/install/pluginsinstallsave/saveBatchOfField.do?&op=forbid&mdCode=${param.mdCode}",
						dataType:'json',
						data:{ids:ids,fieldValue:JSON.stringify({isUsed:0})},
						success:function(data){
							if(data && data.hasOk == true){
								BT.showSuccess("提交成功!",function(){
									$("#listTable").bootstrapTable("refresh");
								});
							}else{
								BT.showError("提交出错!"+data.message);
							}
						}
					});
				}) 
 			}else{
				BT.showWarning("请至少选中一行！"); 
			}
		},
		/* 启用 */
		start: function(){
			var selections = $("#listTable").bootstrapTable('getSelections');
			if(selections.length>0){
				BT.showConfirm("启用","你确定要启用所选的插件吗？",function(){
					var ids = "";
					for(var i=0; i<selections.length; i++){
						if(selections[i].isUsed == 1){
							BT.showWarning("您选择的数据里包括已被启用的插件!");
							return false;
						}
						ids = ids + selections[i].id + ",";
					}
					ids = ids.substring(0,ids.length-1);
					$.ajax({
						type: "POST",
						url:"${baseURL}/install/pluginsinstallsave/saveBatchOfField.do?&op=enable&mdCode=${param.mdCode}",
						dataType:'json',
						data:{ids:ids,fieldValue:JSON.stringify({isUsed:1})},
						success:function(data){
							if(data && data.hasOk == true){
								BT.showSuccess("提交成功!",function(){
									$("#listTable").bootstrapTable("refresh");
								});
							}else{
								BT.showError("提交出错!"+data.message);
							}
						}
					});
				}) 
 			}else{
				BT.showWarning("请至少选中一行！"); 
			}
		},
		//删除
		deleteByIds: function(){
			var selections = $("#listTable").bootstrapTable('getSelections');
			if(selections.length>0){
				BT.showConfirm("删除","你确定删除所选信息吗？",function(){
					var ids = "";
					for(var i=0; i<selections.length; i++){
						ids = ids + selections[i].id + ",";
					}
					ids = ids.substring(0,ids.length-1);
					$.ajax({
						type: "POST",
						url:"${baseURL}/install/pluginsinstalldelete/delete.do?mdId=${param.mdId}&mdCode=${param.mdCode}",
						dataType:'json',
						data:{ids:ids},
						success:function(data){
							if(data && data.hasOk == true){
								BT.showSuccess("删除成功!",function(){
									$("#listTable").bootstrapTable("refresh");
								});
							}else{
								BT.showError("操作出错!"+data.message);
							}
						}
					})
				}) 
 			}else{
				BT.showWarning("请至少选中一行！"); 
			}
		},
		main: function(){
			var _this = StandardTable;
			_this.initTable();
			initSearchGrid($('#listTable'));
		}
	}
	
	$(function () {
		// 渲染选择器
		var defaultParams = [];
		<c:if test="${param.defaultParams!=null}">
	    var params = JSON.parse('${param.defaultParams}'||'{}');
	    for(var key in params){
	    	defaultParams.push({name:key,value:params[key],type:"exact-match"});
	    }
	    </c:if>
		var selectorData = JSON.parse('${module.selectorData}'||"[]",function(k,v){
			  if(v.indexOf&&v.indexOf('function')>-1){
				  return eval("(function(){return "+v+" })()");
			  }
			  return v;
			 });
		if (selectorData.length > 0) {
			var searchCondition = JSON.parse('${module.searchCondition}'||"[]");
			var searchGrid = {
				$element: $('#listTable'),
				load: function(){
					this.$element.bootstrapTable('refresh');
				}
			}
			selector = $.initSelector(searchGrid, selectorData, defaultParams, searchCondition);
		} else {
			$("#standardSearchForm").hide();
		}
		initListTable({
			url:"/platform/plugins/platformpluginslist/listJSON.do",
			module: {
				mdId: '${param.mdId}',
				mdCode: '${param.mdCode}',
				moduleDisplay: '${module.moduleDisplay}'
			},
			tableParams:{
				queryParams: function(params) {
					var data = [];
					if(selector != null){
						data = selector.getSelectorValue();
					}
					return {
						pageNo: params.pageNumber,
						perPageSize: params.pageSize,
						searchParam: JSON.stringify(data)
					}
				}
			}
		});
	});
	
	
	// 该方法用于给编辑页面刷新本页面
	function refreshTable(){
		$('#listTable').bootstrapTable('refresh'); 
	}
	
	var uploadUrl = '${baseURL}/plateform/resource';
	var platUrl = '${baseURL}/plateform/plugin/platformplugin';
	/**
	  *初次下载插件开始
	  */
	  //选择项目相关插件
	  function FF_downloadFromCloudEvent(){
		  	BootstrapDialog.show({
				title: '插件信息选择框',
				size: 1000,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': uploadUrl + 'downloadlist/list.do?type=1'
			    },
		        buttons: [ {
		            label: '选择版本',
		            cssClass: 'btn-primary',
		            action: function(dialogItself){
		            	  var row = modulegrid.getLastSelected();
						  if(row == null){
							  win.$.ligerDialog.warn("请选择需要下载的插件！"); 
							  return false;
						  }else if(row.isLocal){
							  win.$.ligerDialog.warn("本地已存在插件，不允许重复下载！"); 
							  return false;
						  }
						  dialogItself.close();
						  openModuleVersionWin(row);
		            }
		         }, {
		            label: '取消',
		            action: function(dialogItself){
		                dialogItself.close();
		            }
		        }] 
			});
	  }
	 
	  var moduleVersionWin = null;
	  function openModuleVersionWin(row){
		  moduleVersionWin = BootstrapDialog.show({
				title: '插件版本选择框',
				size: 1000,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': uploadUrl + 'downloadlist/versionList.do?type=1&resourceId='+row.resourceId
			    },
		        buttons: [ {
		            label: '确认',
		            cssClass: 'btn-primary',
		            action: function(dialogItself){
		            	var version = downloadVersionGrid.getLastSelected();
						  if(version == null){
							  win.$.ligerDialog.warn("请选择需要下载的版本！"); 
							  return false;
						  }
						  downloadResourceAndCover(version);  
		            }
		         },{
			            label: '返回',
			            action: function(dialogItself){
			            	dialogItself.close();
  						    FF_downloadFromCloudEvent(); 
			            }
			      },{
		            label: '取消',
		            action: function(dialogItself){
		                dialogItself.close();
		            }
		        }] 
			});
	  }
	  
	  function downloadResourceAndCover(version){
 		  //初次下载执行更新覆盖
   		   updateResource(2,version,null);
	  }
	  
  	  /**
	   *====================================初次下载插件结束======================================
	   */
	  //选择版本弹出框
	  var versionWin ; 
	  
	  //更新模块
	  function FF_UpdateClickEvent(btnItem){
		  var selection = $("#listTable").bootstrapTable('getSelections');
		  if(selection.length == 0){
			  win.$.ligerDialog.warn("请选择需要更新的插件"); return false;
		  }
		  var row = selection[0];
		  
		  versionWin = BootstrapDialog.show({
				title: '版本信息选择框',
				size: 1000,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': uploadUrl + 'versionlist/list.do?type=1&resourceId=' + row.id
			    },
		        buttons: [ {
		            label: '下载文件',
		            cssClass: 'btn-primary',
		            action: function(dialogItself){
		            	updateResource(0,null,row.id);
		            }
		         }, {
		            label: '覆盖更新',
		            action: function(dialogItself){
		            	updateResource(1,null,row.id);
		            }
		         }, {
		            label: '取消',
		            action: function(dialogItself){
		                dialogItself.close();
		            }
		         }] 
			});
	  }
	  
	  //更新插件
	  function updateResource(updateType,row,resourceId){
		  var version;
		  if(row != null && row != undefined ){
			   version = row;
		  }else if(versionWin != null){
			  version = versiongrid.getLastSelected();
		  }
 		  if(!version) { win.$.ligerDialog.warn("请选择更新版本"); return false;}
 		  if(resourceId == null || resourceId == undefined )
 			 resourceId = "";
		  //请求云中心下载版本文件
		  badousoft.ajax({
				type: "post",
				url: uploadUrl+"update/updateResource.do",
				dataType : "json",
  				data: "versionId=" + version.id + "&resourceId=" + resourceId + "&versionCode=" + version.versionCode + "&version1=" + version.version1 + "&version2=" + version.version2 + "&version3=" + version.version3 +"&platformResourceId="+version.platformResourceId+ "&type=1&operType=1&updateType="+updateType,
				beforeSend: function (){
	                LG.loading = true;
	                LG.showLoading('请销后...');
	            },
	            complete: function (){
	                LG.loading = false;	  
	                LG.hideLoading();
	            },
				success: function(msg){
 					if(msg && msg.hasOk){
 						//请求成功，下载文件
 						downloadResource(msg.bean);
 					}else{
 						win.$.ligerDialog.error(msg.message);
 					}
				}
			});
		  if(versionWin != null)
		 	 versionWin.close();
		  if(moduleVersionWin != null)
			  moduleVersionWin.close();
	  }
	  
	  //下载文件
	  function downloadResource(resourceId){
		//执行覆盖操作
       	 badousoft.ajax({
				type: "post",
				url: uploadUrl+"update/downloadResource.do",
				dataType : "json",
				data: "type=1&resourceId=" + resourceId,
				beforeSend: function (){
	                LG.loading = true;
	                LG.showLoading('请销后...');
	            },
	            complete: function (){
	                LG.loading = false;	  
	                LG.hideLoading();
	            },
				success: function(msg){
					if(msg && msg.hasOk){
						refreshTable();
						win.$.ligerDialog.success('覆盖成功！');
						versionWin.close();
					}else{
						win.$.ligerDialog.error(msg.message);
					}
				}
		  });
	  }
	  
	  //复制模块
	  function FF_CopyClickEvent(btnItem){
		  var selection = $("#listTable").bootstrapTable('getSelections');
		  if(selection.length == 0){
			  win.$.ligerDialog.warn("请选择需要更新的插件"); return false;
		  }
		  var row = selection[0];
	      if (!row) { win.$.ligerDialog.warn("请选择需要复制的数据"); return false;}
	      jQuery.ligerDialog.confirm('即将复制该模块，请稍后自行更改模块名称等信息  !', function (confirm) {
              if (confirm){
              	 badousoft.ajax({
   					type: "post",
   					url: baseUrl+"copysave/copymodule.do",
   					dataType : "json",
   					data: "moduleId="+row.id,
   					beforeSend: function (){
		                LG.loading = true;
		                LG.showLoading('请销后...');
		            },
		            complete: function (){
		                LG.loading = false;	  
		                LG.hideLoading();
		            },
   					success: function(msg){
     					if(msg && msg.hasOk){
     						refreshTable();
     						win.$.ligerDialog.success('复制成功');
     					}else{
     						win.$.ligerDialog.error(msg.message);
     					}
   					}
 				 });
              }
	       });
	  }
     
     var submitCloudlWin = null;
	  //提交插件到云
	  function FF_SubmitToCloudEvent(){
		  var selection = $("#listTable").bootstrapTable('getSelections');
		  if(selection.length == 0){
			  win.$.ligerDialog.warn("请选择需要更新的插件"); return false;
		  }
		  var row = selection[0];
		  if (row.uploadState == 1) { win.$.ligerDialog.warn("数据上传中，请稍后再试"); return false;}
		  submitCloudlWin = BootstrapDialog.show({
				title: '提交到云平台',
				size: 1000,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': uploadUrl + 'versionlist/upload.do?type=1&resourceId=' + row.id
			    },
		        buttons: [ {
		            label: '保存',
		            cssClass: 'btn-primary',
		            action: function(dialogItself){
		            	FF_SubmitToCloud();
		            }
		         }, {
		            label: '取消',
		            action: function(dialogItself){
						$("#errorVersionMsgFont").text("");
						$(':input','#submitToCloudForm').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
						dialogItself.close();
		            }
		         }] 
			});
		}
		//这个是提交云保存方法
		function FF_SubmitToCloud(){
			var remark = $("#cloud_remark").val();
			if(remark == null || remark == ""){
				win.$.ligerDialog.error('请填写备注!');
				return;
			}
			var resourceId = $("#cloud_resourceId").val();
			//判断下当前提交的是否云中心资源表中的版本标签
			var errorVersion = $("#errorVersion").val();
			var versionCode = $("#cloudVersionSelect option:selected").text();
			if(null!=errorVersion && errorVersion!="" && errorVersion==versionCode){
				win.$.ligerDialog.warn('当前版本与云中心不一致，请先执行更新操作！');
				return;
			}
			//如果是新增
			var isNew = 0;
			var versionStr = $("#cloudVersionSelect option:selected").val();
			if("addNewVersion" == versionStr){
				versionStr = $("#cloud_versionCode").val()+"_"+$("#cloud_version1").val()+"_"+$("#cloud_version2").val()+"_"+$("#cloud_version3").val();
				isNew = 1;
			}
			
			$.ajax({
	    		url: uploadUrl+'upload/uploadResourceData.do',
	    		type: 'post',
	    		data: {resourceId:resourceId,versionStr:versionStr,remark:$("#cloud_remark").val(),isNew:isNew,type:1},
	    		dataType: 'json',
	    		beforeSend: function (a, b, c){
	            	loader.open($('body'));
	            },
	            complete: function (){
	            	loader.close($('body'));
	            },
	    		success: function(msg) {
	    			if(msg && msg.hasOk){
						win.$.ligerDialog.success('上传中');
						submitCloudlWin.close();
						$("#errorVersionMsgFont").text("");
						$(':input','#submitToCloudForm').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
					    
						$("#cloud_version1").val(0);
						$("#cloud_version2").val(0);
						$("#cloud_version3").val(1);
						
	    			}else{
						win.$.ligerDialog.error(msg.message);
					}
	    		},
	    		error: function(msg) {
	    			win.$.ligerDialog.error(msg);
	    		}
	    	});
		}
	</script>
</body>
</html>